package com.dys.loginhander;

import com.dys.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @className: com.dys.loginhander.LoginServlet
 * @description: 用来向main.html中跳转的, 同时验证登录, 登录过, 可以直接跳转, 否则回到登录页
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/20 22:50
 */
@WebServlet(urlPatterns = "/mainServlet.do")
public class MainServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 跳转到main.html
        HttpSession httpSession = req.getSession();
        User user = (User) httpSession.getAttribute("user");
        if (user != null) {
            // 判断如果登录过 允许跳转  HttpSession中如果有登陆过的信息
            req.getRequestDispatcher("/WEB-INF/main.html").forward(req, resp);
        } else {
            // 如果没有登录过 回到登录去登录 HttpSession中如果有登陆过的信息
            resp.sendRedirect("login.html");
        }
    }
}
